import CURDHelper from '@/utils/CURDHelper'
import { VxeUI } from 'vxe-pc-ui';
import PageConfig from '@/utils/pageConfig'
import Query from '@/components/Query'
import XEUtils from 'xe-utils'
import { getBtnCtr } from '@/utils/BtnControl'

export default {
  name: 'ReportCertificate',
  components: {
    Query
  },
  data() {
    const gltxItemRender = {
      name: 'VxeSelect',
      props:{
        filterable: true,
      },
      options: [],
    }
    const khItemRender = {
      name: 'VxeSelect',
      props:{
        filterable: true,
      },
      options: [] 
    } 
    return {
      gltxItemRender,
      khItemRender,
      baseApiUrl: '/api/Report',
      componentSize: PageConfig.ComponentSize,
      duration: PageConfig.Duration,
      QueryConditions: [],
      btnPromission: undefined,
      editModal:false,
      QueryConfig: [{
        title: '客户名称',
        field: 'khmc',
        type: 'input',
        value: '',
        placeholder: '请输入客户名称以查询',
      }, {
        title: '体系名称',
        field: 'txmc',
        type: 'input',
        value: '',
        placeholder: '请输入体系名称以查询'
      }, {
        title: '待审核',
        field: 'auditing',
        type: 'daterange',
        date1: undefined,
        date2: undefined,
        placeholder1: '请选择待审核开始时间',
        placeholder2: '请选择待审核结束时间'
      }],
      GridOptions: {
        id: 'ReportCertificate_MainGrid',
        border: true,
        stripe: true,
        loading: false,
        size: PageConfig.ComponentSize,
        height: 700,
        columnConfig: {
          resizable: true
        },
        rowConfig: {
          isHover: true,
          isCurrent: true
        },
        checkboxConfig: {
          highlight: true,
          range: true
        },
        columns: [
          { type: 'seq', width: 50 },
          { type: 'checkbox', width: 35 },
          { field: 'khmc', title: '客户名称', sortable: true, minWidth: 120, showOverflow: true },
          { field: 'txmc', title: '体系名称', sortable: true, minWidth: 120, showOverflow: true },
          { field: 'zsh', title: '证书号', sortable: true, minWidth: 120, showOverflow: true },
          { field: 'zsrq', title: '证书开始日期', sortable: true, minWidth: 120, showOverflow: true ,
            formatter: ({cellValue}) => XEUtils.toDateString(cellValue,'yyyy-MM-dd')
          },
          { field: 'zsyxq', title: '证书结束日期', sortable: true, minWidth: 120, showOverflow: true ,
            formatter: ({cellValue}) => XEUtils.toDateString(cellValue,'yyyy-MM-dd')
          },
          { field: 'mcshrq', title: '末次审核日期', sortable: true, minWidth: 120, showOverflow: true ,
            formatter: ({cellValue}) => XEUtils.toDateString(cellValue,'yyyy-MM-dd')
          },
          { field: 'xcshrq', title: '下次审核日期', sortable: true, minWidth: 120, showOverflow: true ,
            formatter: ({cellValue}) => XEUtils.toDateString(cellValue,'yyyy-MM-dd')
          },
          { field: 'dshlx', title: '待审核类型', minWidth: 100, showOverflow: true ,filters: [{ data: '' }], filterRender: {name:'VxeInput'}},
          { field: 'operation', title: '操作', fixed: 'right', width: 60, slots: { default: 'operation' } },
        ],
        data: []
      },
      PagerConfig: {
        align: 'center',
        pageSize: 25,
        total: 0,
        currentPage: 1,
        pageSizes: [10, 25, 50, 100, 200],
      },
      FormOptions: {
        'title-width': 100,
        border: true,
        titleBackground: true,
        data: {
          id: -1,
          createid: '',
          createtime: '',
          createname: '',
          updateid: '',
          updatetime: '',
          updatename: '',
          khid: undefined,
          txid: undefined,
          erpbh: '',
          sf:'',
          sxq: '',
          shlx: '',
          rzfw: '',
          txrs: '',
          csje: undefined,
          jsje: undefined,
          htqdrq: undefined,
          bz: '',
          htje:undefined,
          sfdcs:undefined,
          dcslb:undefined,
          htlx:undefined,
          htdz:undefined,
        },
        rules: {
          txid: [{ required: true, message: '请选择体系名称', trigger: 'blur' }],
          khid: [{ required: true, message: '请选择客户信息', trigger: 'blur' }],
          shlx: [{ required: true, message: '请选择审核类型', trigger: 'blur' }],
          erpbh: [{ required: true, message: '请输入合同号', trigger: 'blur' }],
          rzfw: [{ required: true, message: '请选择认证范围', trigger: 'blur' }],
          txrs: [{ required: true, message: '请输入体系人数', trigger: 'blur' ,validator: this.ValidatorNumber}],
          csje: [{ required: true, message: '请输入初审金额', trigger: 'blur' ,validator: this.ValidatorNumber}],
          htje: [{ required: true, message: '请输入合同金额', trigger: 'blur' ,validator: this.ValidatorNumber}],
          jsje: [{ required: true, message: '请输入监审金额', trigger: 'blur' ,validator: this.ValidatorNumber}],
          htqdrq: [{ required: true, message: '请选择合同日期', trigger: 'blur' }],
        },
        items: [
          { field: 'txid', title: '体系名称', span: 12, itemRender: gltxItemRender, placeholder: '请选择体系名称' },
          { field: 'khid', title: '客户名称', span: 12, itemRender: khItemRender, placeholder: '请选择关联体系' },
          { field: 'erpbh', title: '合同号', span: 12, itemRender:{name:'VxeInput'}, placeholder: '请输入合同号' },
          { field: 'htlx', title: '合同类型', span: 12, itemRender: { name: 'VxeSelect', 
            options: [{ label: '初审', value: '初审' }, { label: '监一', value: '监一' },
              { label: '监二', value: '监二'},{ label: '再认证', value: '再认证'}] }, placeholder: '请选择合同类型' },
          { field: 'sf', title: '省份', span: 12, itemRender:{name:'VxeInput'}, placeholder: '请输入省份' },
          { field: 'sxq', title: '区域', span: 12, itemRender:{name:'VxeInput'}, placeholder: '请输入区域' },
          { field: 'shlx', title: '审核类型', span: 12, itemRender: { name: 'VxeSelect', 
            options: [{ label: '初认证', value: '初认证' }, { label: '监一', value: '监一' },
              { label: '监二', value: '监二'},{ label: '再认证', value: '再认证'},
              { label: '扩大', value: '扩大'},{ label: '证书变更', value: '证书变更'}] }, placeholder: '请选择审核类型' },
          { field: 'txrs', title: '体系人数', span: 12, placeholder: '请输入体系人数', 
            itemRender: { name: 'VxeInput', clearable: true, placeholder: '请输入体系人数' } },
          { field: 'rzfw', title: '认证范围', span: 24, itemRender: {  name: 'VxeTextarea', maxlegnth: 500, clearable: true, 
            showWordCount: true }},
          { field: 'csje', title: '初审金额', span: 12, placeholder: '请输入初审金额',
            itemRender: { name: 'VxeInput',  clearable: true, placeholder: '请输入初审金额' ,
              events:{ change:e =>{this.FormOptions.data.htje = this.FormOptions.data.csje}}} },
          { field: 'jsje', title: '监审金额', span: 12, placeholder: '请输入监审金额',
            itemRender: { name: 'VxeInput',  clearable: true, placeholder: '请输入监审金额' } },
          { field: 'htje', title: '合同金额', span: 12, placeholder: '请输入合同金额',
              itemRender: { name: 'VxeInput',  clearable: true, placeholder: '请输入合同金额' } }, 
          { field: 'htqdrq', title: '合同日期', span: 12, itemRender: { name: 'VxeDatePicker', type: 'date', valueFormat: 'YYYY-MM-DD', 
            placeholder: '请选择签单日期' } },
          { field: 'sfdcs', title: '是否多场所', span: 12, itemRender: { name: 'VxeSelect', 
            options: [{ label: '', value: '' },{ label: '是', value: '是' }, { label: '否', value: '否' }] }, placeholder: '请选择是否多场所'},
          { field: 'dcslb', title: '多场所类别', span: 12, itemRender: { name: 'VxeSelect', 
             options: [{ label: '', value: '' },{ label: '延伸', value: '延伸' }, { label: '支持', value: '支持' }],clearable: true }, placeholder: '请选择多场所类别'},
          { field: 'zydm', title: '专业代码', span: 24, itemRender: { name: 'VxeInput', clearable: true, placeholder: '请输入专业代码'}}, 
          { field: 'htdz', title: '合同地址', span: 24, itemRender: { name: 'VxeInput', clearable: true, placeholder: '请输入合同地址'}}, 
          { field: 'bz', title: '备注', span: 24, itemRender: { name: 'VxeTextarea', maxlegnth: 500, clearable: true, showWordCount: true }, 
            placeholder: '请输入备注' },
          {
            align: 'center',
            span: 24,
            itemRender: {
              name: 'VxeButtonGroup',
              options: [{
                type: 'submit', content: '提交', status: 'primary'
              }, {
                type: 'reset', content: '重置'
              }]
            }
          }
        ]
      },
    }
  },
  created() {
    this.btnPromission = getBtnCtr(
          this.$route.path,
          this.$store.getters.userPermission
        )
        this.GetTxOptions();
        this.GetKhOptions();
  },
  methods: {
    handleQuery(e) {
      this.QueryConditions = e;
      this.DoQuery(e);
    },
    handlePageChange() {
      this.DoQuery(this.QueryConditions);
    },
    handleCreate(row) {
      row.dshlx == '再认证' ? this.DoAdd(row) : this.DoCreate(row);
    },
    handleEditModalSubmit() {
      this.DoAddData();
    },
    handleEditModalReset() {

    },
    DoQuery(e) {
      let request = {
        isPaging: false,
        pageSize: this.PagerConfig.pageSize,
        pageIndex: this.PagerConfig.currentPage,
        queryConditions: e,
        orderByConditions: []
      }
      CURDHelper.Query(request, `${this.baseApiUrl}/GetCertificateReport`)
        .then(res => {
          this.GridOptions.data = res.data;
          this.PagerConfig.total = res.totalNumber;

        }).catch(err => {
          VxeUI.modal.message({
            content: err,
            status: 'error',
            duration: this.duration
          })
        })
    },
    DoAdd(row){
      let query = [
        {column:'khid',condition:0,content:row.khid},
        {column:'txid',condition:0,content:row.txid},
        {column:'zsh',condition:0,content:row.zsh},
      ]
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: query,
        orderByConditions: []
      }
      CURDHelper.Query(request, '/api/xmglxmxx')
        .then(res => {
          if(res.data && res.data.length > 0){
            this.FormOptions.data = res.data[0];
            this.FormOptions.data.erpbh = '';
            this.FormOptions.data.shlx = '再认证';
            this.FormOptions.data.shzt = 'N';
            this.FormOptions.data.shr = null;
            this.FormOptions.data.shsj = null;
            this.FormOptions.data.pczt = 'N';
            this.FormOptions.data.pcshr = null;
            this.FormOptions.data.pcshsj = null;
            this.FormOptions.data.pcshzt = 'N';
            this.FormOptions.data.xmzt = 'N';
            this.FormOptions.data.xmztqrr = null;
            this.FormOptions.data.xmztqrsj = null;
            this.FormOptions.data.skzt = 'N';
            this.FormOptions.data.skqrr = null;
            this.FormOptions.data.skqrsj = null;
            this.FormOptions.data.kpzt = 'N';
            this.FormOptions.data.kpqrr = null;
            this.FormOptions.data.kpqrsj = null;
            this.FormOptions.data.zsh = null;
            this.FormOptions.data.zsksrq = null;
            this.FormOptions.data.zsjsrq = null;
            this.FormOptions.data.zslb = null;
            console.log(this.FormOptions.data);
          }
          this.editModal = true;
        }).catch(err => {
          VxeUI.modal.message({content: `${err}`,status: 'error',duration: this.duration})
        })
    },
    DoAddData() {
      //新增
      CURDHelper.Add(this.FormOptions.data, null, '/api/xmglxmxx')
        .then(res => {
          VxeUI.modal.message({content: '新增项目信息成功',status: 'success',duration: this.duration})         
          this.editModal = false;
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
    DoCreate(row) {
      //新增
      let postModel = {
        khid: row.khid,
        txid: row.txid,
        zsh: row.zsh,
        shxh: row.dshlx == '监一' ? 1 : 2
      }
      CURDHelper.AddWithConfirm("是否确定并生成项目信息?",postModel, null, '/api/Xmglxmxx/Create')
        .then(res => {
          VxeUI.modal.message({ content: '生成项目信息成功，请到项目管理继续操作', status: 'success', duration: this.duration })
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
    GetTxOptions() {
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: [],
        orderByConditions: []
      }
      CURDHelper.Query(request, '/api/Xmjcgltx')
        .then(res => {
          this.gltxItemRender.options = [];
          res.data.forEach(item => {
            this.gltxItemRender.options.push({
              value: item.id,
              label: item.txmc
            })
          })
        }).catch(err => {
          VxeUI.modal.message({content: `${err}`,status: 'error',duration: this.duration})
        })
    },
    GetKhOptions() {
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: [],
        orderByConditions: []
      }
      CURDHelper.Query(request, '/api/Xmjckhxx')
        .then(res => {
          this.khItemRender.options = [];
          res.data.forEach(item => {
            this.khItemRender.options.push({
              value: item.id,
              label: item.khmc
            })
          })
        }).catch(err => {
          VxeUI.modal.message({content: `${err}`,status: 'error',duration: this.duration})
        })
    },
  }
}
